package com.whz.parameterized;

import java.util.function.UnaryOperator;

/**
 * @Author: sinosoft
 * @Description: 恒等函数分发器
 * @Date: Create in 18:47 2021/5/11
 * @Modified By:
 */
public class IdentityFunctionUtils {
    private static UnaryOperator<Object> IDENTITY_FN = (t) -> t;
    @SuppressWarnings("unchecked")
    public static <T> UnaryOperator<T> identityFunction() {
        return (UnaryOperator<T>) IDENTITY_FN;
    }

    public static void main(String[] args) {
        String[] strings = {"jute", "hemp", "nylon"};
        UnaryOperator<String> sameString = identityFunction();
        for (String s : strings) {
            System.out.println(sameString.apply(s));
        }

        Number[] numbers = {1, 2.0, 3L};
        UnaryOperator<Number> sameNumber = identityFunction();

        for(Number n : numbers) {
            System.out.println(sameNumber.apply(n));
        }
    }
}
